// app.js
App({
  d: {
    userId: 1,
    hostUrl: 'https://read.kuajiewl.com/',
    goodsurl: 'upload/goods/prime/0/',
    ablumurl: 'upload/goods/album/0/',
    userInfo:[]
  },
  onLaunch: function () {

  },
  getStore:function(cb){

    var that = this
    if (this.globalData.store) {
      typeof cb == "function" && cb(this.globalData.store)
    } else {
      wx.request({
        url: that.d.hostUrl + 'wapi/index.html',
        method: 'post',
        data: {  },
        header: {
          'Content-Type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {
          wx.setNavigationBarTitle({
            title: res.data.lat.site_name
          })
        },
        fail: function (e) {
          wx.showToast({
            title: '网络异常！',
            duration: 2000
          });
        },
      })
    }
    
  },
  getUserInfo:function(cb){
    var that = this;
    if(this.globalData.userInfo){
      typeof cb == "function" && cb(this.globalData.userInfo)
    } else {
      //调用登录接口
      wx.login({
        success: function (res) {
          var code = res.code;
          //get wx user simple info
          wx.getUserInfo({
            success: function (res) {
              that.globalData.userInfo = res.userInfo
              typeof cb == "function" && cb(that.globalData.userInfo);

              that.onLoginUser(code);
            },
            fail: res => {
              //console.log(1111);
            }
          });
        }
      });
    }
  },

  getUserSessionKey:function(code){
    //用户的订单状态
    var that = this;
    wx.request({
      url: that.d.hostUrl + 'wapi/getsessionkey.html',
      method:'post',
      data: {
        code: code
      },
      header: {
        'Content-Type':  'application/x-www-form-urlencoded'
      },
      success: function (res) {
        //--init data        
        var data = res.data;
        if(data.status > 0){
          wx.showToast({
            title: data.err,
            duration: 2000
          });
          wx.showToast({
            title: '登录错误',
            duration: 2000
          });
          return false;
        }
        that.globalData.userInfo['sessionId'] = data.session_key;
        that.globalData.userInfo['openid'] = data.openid;
      },
      fail:function(e){
        wx.showToast({
          title: '网络异常！err:getsessionkeys',
          duration: 2000
        });
      },
    });
  },
  get_store:function(){
    var that = this;
    var store = wx.getStorageSync('store');
    that.globalData.store = store;
    typeof cb == "function" && cb(that.globalData.store)
  },
  onLoginUser:function(code){
    var that = this;
    var user = that.globalData.userInfo;
    var location = that.globalData.locationinfo;
    wx.request({
      url: that.d.hostUrl + 'wapi/authlogin.html',
      method:'post',
      data: {
        code: code,
        gender:user.gender,
        nickName: user.nickName,
        headUrl: user.avatarUrl,
      },
      header: {
        'Content-Type':  'application/x-www-form-urlencoded'
      },
      success: function (res) {
        var data = res.data.data;
        var status = res.data.ret;
        if(status!=0){
          wx.showToast({
            title: res.data.msg,
            duration: 3000
          });
          return false;
        }

        that.globalData.phone = data.mobile;
        that.globalData.vip = data.vip;
        that.globalData.bank = data.bank;
        that.globalData.bank_name = data.bank_name;
        that.globalData.sessionid = data.sessionid;
        that.globalData.openid = data.openid;
        that.globalData.user_id = data.user_id;
        if (that.phoneCallback) {
          that.phoneCallback(data.mobile);
        }
        if (that.vipCallback) {
          that.vipCallback(data.vip);
        }
   
        if (that.bankCallback) {
          that.bankCallback(data.bank);
        }
        if (that.bank_nameCallback) {
          that.bank_nameCallback(data.bank_name);
        }
        if (that.openidCallback) {
          that.openidCallback(data.openid);
        }
        if (that.sessionidCallback) {
          that.sessionidCallback(data.sessionid);
        }
        if (that.user_idCallback) {
          that.user_idCallback(data.user_id);
        }
        
        that.globalData.userInfo['id'] = data.user_id;
        that.globalData.userInfo['NickName'] = data.nickName;
        that.globalData.userInfo['phone'] = that.d.userId = data.mobile;
        that.globalData.userInfo['HeadUrl'] = data.avatarUrl;
        that.globalData.userInfo['sessionId'] = data.sessionid;
        that.globalData.userInfo['openid'] = data.openid;
        
        var userId = data.user_id;
        if (!userId){
          wx.showToast({
            title: '登录失败！',
            duration: 3000
          });
          return false;
        }
        that.d.userId = userId;
      },
      fail:function(e){
        wx.showToast({
          title: '网络异常！err:',
          duration: 2000
        });
      },
    });
  },
  getOrBindTelPhone:function(returnUrl){
    var user = this.globalData.userInfo;
    if(!user.tel){
      wx.navigateTo({
        url: 'pages/binding/binding'
      });
    }
  },
 globalData:{
   userInfo: null,
   locationinfo: null,
   store: null,
   lat_lnt:null,
   canorder:1,
   phone: '',
   vip:0,
   bank:'',
   bank_name:'',
   openid:'',
   sessionId:'',
  },

});





